// 设置默认信息
$.validator.setDefaults({
  submitHandler: function (val) {
        oktipsoption = {
            type: 1,
            url: $('.formsub').attr('url'),
            msg: '提交成功'
        };
        ajaxform.init(oktipsoption, '.formsub');
        ajaxform.submit();
    },
    errorPlacement: function (error, element) {
        element.parents('.l_b').append(error);
    }
});

$(document).ready(function () {

   $('#cust_register_date').datepicker({
        dateFormat: 'yy-mm-dd',
        changeYear:true,
        changeMonth:true,
        maxDate:new Date()
   });

    $(".l_b input:not(.read_only)").focus(function () {
        $(this).addClass('u-ipt-active');
        $(this).removeClass('error');
        $(this).next('.error').hide();
    });


    $(".l_b input:not(.read_only)").blur(function () {
        $(this).removeClass('u-ipt-active');
        if ($(this).parents('.l_b').find('label.error').is(":visible")) {
            $(this).addClass('error');
            $(this).parents('.l_b').find('.prompt').hide();
        } else {
            $(this).removeClass('error');
            $(this).removeClass('u-ipt-active');
            $(this).parent('.file_name').removeClass('error');
            $(this).parents('.l_b').find('.prompt').show();
        }
    });

   // 限制只能输入数字
   // 大于等于1，小于等于99999999之间的整数
    var record;
    $("#staff_num").on('keyup', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^[1-9][0-9]*$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

    $("#dkk_kh").on('keyup', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^[0-9][0-9]*$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

  // 范围要求在大于等于0，小于等于9999999999.99之间
 $("#register_capital,#xssr_snd,#zzc_snd,#zfz_snd,#xscb_snd,#other_bank_limit,#apply_amt").on('keyup', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^\d{0,999}\.{0,1}(\d{1,2})?$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

// 贷款余额范围要求在大于等于-9999999999.99，小于等于9999999999.99之间
 $("#jhdkye").on('keyup', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /^(-)?\d{0,999}\.{0,1}(\d{1,2})?$/;
        if (val != '' && decimalReg.test(val)) {
            record = val;
        } else {
            if (val != '') {
                myThis.val(record);
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

// 贷款余额范围要求在大于等于-9999999999.99，小于等于9999999999.99之间
  // $.validator.addMethod("chknum3", function (value) {
  //       if(parseFloat(value)>=-9999999999.99 && parseFloat(value)<=9999999999.99){
  //         return true
  //       }else{
  //         return false
  //       }
  //   });

//组织机构代码
 $("#orgn_num").on('keyup', function () {
        var myThis = $(this);
        var val = myThis.val();
        var decimalReg = /[^a-z0-9A-Z^\uFF00-\uFFFF-^]/g;
        if (val != '' && decimalReg.test(val)) {
            myThis.val(record);
        } else {
            if (val != '') {
                record = val;
            }
        }
        myThis.blur(function () {
            record = '';
        });
    });

  // 验证范围在大于0-9999999999.99之间
  $.validator.addMethod("chknum", function (value) {
        if(parseFloat(value)>0 && parseFloat(value)<=9999999999.99){
          return true
        }else{
          return false
        }
    });

  // 验证范围在等于0-9999999999.99之间
  $.validator.addMethod("chknum2", function (value) {
        if(parseFloat(value)>=0 && parseFloat(value)<=9999999999.99){
          return true
        }else{
          return false
        }
    });

  // 验证申请人手机
  $.validator.addMethod("chkapplicant_phone", function (value)
    {
        var applicant_phoneReg = /^1\d{10}$/;
        return applicant_phoneReg.test(value);
    });

  // 验证申请人申请人固定电话
  $.validator.addMethod("chkapplicant_fix_phone", function (value)
    {
        var applicant_fix_phoneReg = /^0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}$/;
        return applicant_fix_phoneReg.test(value);
    });

  // 验证申请人传真
  $.validator.addMethod("chkapplicant_fax", function (value)
    {
        var applicant_faxReg = /^0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}$/;
        return applicant_faxReg.test(value);
    });


  // 验证企业主身份证号码
    $.validator.addMethod("chkqyz_sfzhm", function (value)
    {
        return check_id($("#qyz_sfzhm").val());
    });

    // 验证组织机构代码
    // $.validator.addMethod("chkorgn_num", function (value) {
    //     var orgn_numReg =/^[a-zA-Z0-9]{8}-[a-zA-Z0-9]$/;
    //     return orgn_numReg.test(value);
    // });

    $("#jhdkye_ind").change(function(){
        $("#finance_applyInfo").validate().element($("#jhdkye"));
        if($(this).val()=='0'){
            $("#jhdkye").addClass('read_only');
            $("#jhdkye").prop('readonly','true');
            $("#jhdkye").parents('td').prev('th').children('span').hide();
            $("#jhdkye").val('');
        }else if($(this).val()=='1'){
            $("#jhdkye").removeClass('read_only');
            $("#jhdkye").removeProp('readonly');
            $("#jhdkye").parents('td').prev('th').children('span').show();
        }
    })
    $("#dkk_ind").change(function(){
       $("#finance_applyInfo").validate().element($("#dkk_kh"));
        if($(this).val()=='0'){
            $("#dkk_kh").addClass('read_only');
            $("#dkk_kh").prop('readonly','readonly');
            $("#dkk_kh").parents('td').prev('th').children('span').hide();
            $("#dkk_kh").val('');
        }else if($(this).val()=='1'){
            $("#dkk_kh").removeClass('read_only');
            $("#dkk_kh").removeProp('readonly');
            $("#dkk_kh").parents('td').prev('th').children('span').show();
        }
    })

     $("#finance_applyInfo").validate({
        rules: {
            customer_name: {
                required: true
            },
            register_address: {
                required: true
            },
            representative: {
                required: true
            },
            register_capital: {
                required: true,
                chknum2:true
                // chkregister_capital:true
            },
            cust_register_date: {
                required: true
            },
            orgn_num: {
                required: true,
                maxlength:10
                // chkorgn_num:true
            },
            enterprise_area: {
                required: true
            },
            staff_num: {
                required: true,
                range:[1,99999999]
            },
            xssr_snd: {
                required: true,
                chknum2:true
            },
            xssr_qnd: {
                required: true,
                chknum2:true
            },
            zzc_snd: {
                required: true,
                chknum2:true
            },
            zfz_snd: {
                required: true,
                chknum2:true
            },
            dkk_kh:{
                 required: {
                    depends: function () { //企业是否有贷款卡为是的时候
                        return ($("#dkk_ind").val()==1);
                    }
                }
            },
            jhdkye:{
                required: {
                    depends: function () { //是否在建行有贷款余额为是的时候
                        return ($("#jhdkye_ind").val()==1);
                    }
                },
                chknum2:{
                    depends: function () { //是否在建行有贷款余额为是的时候
                        return ($("#jhdkye_ind").val()==1);
                    }
                }
            },
            applicant_name: {
                required: true
            },
            applicant_phone: {
                required: true,
                chkapplicant_phone:true
            },
            applicant_fix_phone: {
                required: true,
                chkapplicant_fix_phone:true
            },
            qyz_name: {
                required: true
            },
            qyz_sfzhm: {
                required: true,
                chkqyz_sfzhm:true
            },
            khh_ecl_org_code: {
                required: true
            },
            xscb_snd: {
                required: true,
                chknum2:true
            },
            applicant_fax: {
                required: true,
                chkapplicant_fax:true
            },
            applicant_email: {
                required: true,
                email:true
            },
            apply_amt: {
                required: true,
                chknum:true
            },
            other_bank_limit: {
                required: true
            }
        },
        messages: {
            customer_name: {
                required: '请输入企业名称'
            },
            register_address: {
                required: '请输入注册地址'
            },
            representative: {
                required: '请输入法定代表人'
            },
            register_capital: {
                required: '请输入注册资本（元）',
                chknum2:'范围在0-9999999999.99之间'
                // chkregister_capital:'注册资本在0~9999999999.99之间'
            },
            cust_register_date: {
                required: '请输入工商注册时间'
            },
            orgn_num: {
                required: '请输入组织机构代码',
                maxlength:'组织机构代码不超过10位'
                // chkorgn_num:'请输入正确的组织机构代码'
            },
            enterprise_area: {
                required: '请输入企业实际地区'
            },
            staff_num: {
                required: '请输入员工人数',
                range:'人数在1-99999999之间'
            },
            xssr_snd: {
                required: '请输入上年度销售收入（元）',
                chknum2:'范围在0-9999999999.99之间'
            },
            xssr_qnd: {
                required: '请输入前年销售收入',
                chknum2:'范围在0-9999999999.99之间'
            },
            zzc_snd: {
                required: '请输入截至上年底企业总资产（元）',
                chknum2:'范围在0-9999999999.99之间'
            },
            zfz_snd: {
                required: '请输入截至上年底企业总负债（元）',
                chknum2:'范围在0-9999999999.99之间'
            },
            dkk_kh:{
                required: '请输入贷款卡卡号'
            },
            jhdkye:{
                required: '请输入贷款余额',
                chknum2:'范围在0-9999999999.99之间'
            },
            applicant_name: {
                required: '请输入申请人姓名'
            },
            applicant_phone: {
                required: '请输入申请人手机',
                chkapplicant_phone:'请输入正确的手机号码'
            },
            applicant_fix_phone: {
                required: '请输入申请人固定电话',
                chkapplicant_fix_phone:'如：0511-84457588'
            },
            qyz_name: {
                required: '请输入企业主姓名'
            },
            qyz_sfzhm: {
                required: '请输入企业主身份证号码',
                chkqyz_sfzhm:'请输入正确的身份号'
            },
            khh_ecl_org_code: {
                required: '请输入开户行机构代码'
            },
            xscb_snd: {
                required: '请输入上年度销售成本',
                chknum2:'范围在0-9999999999.99之间'
            },
            applicant_fax: {
                required: '请输入申请人传真',
                chkapplicant_fax:'请输入正确的传真'
            },
            applicant_email: {
                required: '请输入申请人电子邮件',
                email:'请输入正确的电子邮件'
            },
            apply_amt: {
                required: '请输入申请贷款额度',
                chknum:'范围在大于0-9999999999.99之间'
            },
            other_bank_limit: {
                required: '请输入在其他银行已取得的授信额度'
            }
        },
        focusInvalid: false,
        onkeyup: false
    });
    })

function check_id(value)
{
    var arrExp = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];//加权因子  
    var arrValid = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];//校验码  
    if (/^\d{17}\d|x$/i.test(value))
    {
        var sum = 0, idx;
        for (var i = 0; i < value.length - 1; i++)
        {
            // 对前17位数字与权值乘积求和  
            sum += parseInt(value.substr(i, 1), 10) * arrExp[i];
        }
        // 计算模（固定算法）  
        idx = sum % 11;
        // 检验第18为是否与校验码相等  
        return arrValid[idx] == value.substr(17, 1).toUpperCase();
    } else
    {
        return false;
    }
}


